//
//  ZLPTakePhoto.m
//  TangYi
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 赵连鹏. All rights reserved.
//

#define AppRootView  ([[[[[UIApplication sharedApplication] delegate] window] rootViewController] view])

#define AppRootViewController  ([[[[UIApplication sharedApplication] delegate] window] rootViewController])

#import "ZLPTakePhoto.h"

@implementation ZLPTakePhoto

{
    NSInteger sourceType;
}

+ (ZLPTakePhoto *)shareModel {
    static ZLPTakePhoto *shareModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        shareModel = [[self alloc] init];
        
    });
    return shareModel;
}

+ (void)sharePicture:(sendPictureBlock)block {
    ZLPTakePhoto *tP = [ZLPTakePhoto shareModel];
    
    tP.sPictureBlock = block;
    
    UIActionSheet *sheet;
    // 判断是否支持相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        sheet = [[UIActionSheet alloc] initWithTitle:@"设置图片" delegate:tP cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照", @"从相册中选取", nil];
    } else {
        
        NSLog(@"111111");
        
        sheet = [[UIActionSheet alloc] initWithTitle:@"设置图片" delegate:tP cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册中选取", nil];
    }
    sheet.tag = 255;
    
    [sheet showInView:AppRootView];
}

#pragma mark - action sheet协议方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (actionSheet.tag == 255) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    return;
                case 1:// 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 2:// 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                default:
                    break;
            }
        } else {
            if (buttonIndex == 0) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            }
        }
        
        // 跳转到相机/相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        
        [AppRootViewController presentViewController:imagePickerController animated:YES completion:NULL];
        
    }
}


#pragma mark - image picker 协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    ZLPTakePhoto *TPhoto = [ZLPTakePhoto shareModel];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    [TPhoto sPictureBlock](image);
}


@end
